﻿@model DiscountModel.AddCollectionToDiscountModel
@inject AdminAreaSettings adminAreaSettings

@{
    Layout = "";

    //page title
    ViewBag.Title = Loc["admin.marketing.Discounts.AppliedToCollections.AddNew"];
}

<form id="CollectionAddPopup" asp-area="@Constants.AreaAdmin" asp-controller="Discount" asp-action="CollectionAddPopup" method="post"
      asp-route-DiscountId="@HtmlExtensions.HtmlEncodeSafe(Context.Request.Query["DiscountId"])"
      asp-route-btnId="@HtmlExtensions.HtmlEncodeSafe(Context.Request.Query["btnId"])"
      asp-route-formId="@HtmlExtensions.HtmlEncodeSafe(Context.Request.Query["formId"])">


    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-cubes"></i>
                        @Loc["admin.marketing.Discounts.AppliedToCollections.AddNew"]
                    </div>
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="main-header col-12 px-0 mb-3">
                                <div class="row align-items-end">
                                    <div class="col-md-8 col-sm-12 col-12">
                                        <div class="form-group mb-0">
                                            <admin-label asp-for="SearchCollectionName" class="control-label"/>
                                            <admin-input asp-for="SearchCollectionName"/>
                                        </div>
                                    </div>
                                    <div class="col-md-4 col-sm-12 col-12">
                                        <div class="form-actions">
                                            <div class="btn-group btn-group-sm btn-group-devided">
                                                <button class="btn btn-success filter-submit" id="search-collections">
                                                    <i class="fa fa-search"></i> @Loc["Admin.Common.Search"]
                                                </button>
                                                <button class="btn btn-success" type="submit" name="save">
                                                    <i class="fa fa-check"></i> @Loc["Admin.Common.Save"]
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div id="collections-grid"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        var mfp = $.magnificPopup.instance;
        $("#CollectionAddPopup").submit(function (e) {
            e.preventDefault();
            var form = $(this);
            var url = form.attr('action');
            $.ajax({
                type: "POST",
                url: url,
                data: form.serialize(),
                success: function (data) {
                    if (data == "") {
                        mfp.close();
                        $('#btnRefreshCollections').click();
                    } else {
                        $.magnificPopup.open({
                            items: {
                                src: data,
                                type: 'inline'
                            },
                            callbacks: {
                                open: function () {
                                    $('.mfp-wrap').removeAttr('tabindex');
                                }
                            }
                        });
                    }
                }
            });
        });
        $("#collections-grid").kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("CollectionAddPopupList", "Discount", new { area = Constants.AreaAdmin }))",
                        type: "POST",
                        dataType: "json",
                        data: additionalData
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors"
                },
                error: function(e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                pageSize: @(adminAreaSettings.DefaultGridPageSize),
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            pageable: {
                refresh: true,
                pageSizes: [@(adminAreaSettings.GridPageSizes)]
            },
            editable: {
                confirmation: false,
                mode: "inline"
            },
            scrollable: false,
            columns: [{
                field: "Id",
                headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                headerAttributes: { style: "text-align:center" },
                template: "<input type='checkbox' name='SelectedCollectionIds' value='#=Id#' class='checkboxGroups' />",
                attributes: { style: "text-align:center" },
                width: 50
            }, {
                field: "Name",
                title: "@Loc["Admin.Catalog.Collections.Fields.Name"]"
            }, {
                field: "Published",
                title: "@Loc["Admin.Catalog.Collections.Fields.Published"]",
                width: 100,
                headerAttributes: { style: "text-align:center" },
                attributes: { style: "text-align:center" },
                template: '# if(Published) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
            }]
        });
            $('#search-collections').click(function() {
                var grid = $('#collections-grid').data('kendoGrid');
                grid.dataSource.page(1); //new search. Set page size to 1
                $('.checkboxGroups').prop('checked', false).change();
                return false;
            });

            $("#@Html.IdFor(model => model.SearchCollectionName)").keydown(function(event) {
                if (event.keyCode == 13) {
                    $("#search-collections").click();
                    return false;
                }
            });

            //wire up checkboxes
            $('#mastercheckbox').click(function() {
                $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
            });

            $('#collections-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function(e) {
                updateMasterCheckbox();
            });

        function additionalData() {
            var data = {
                SearchCollectionName: $('#@Html.IdFor(model => model.SearchCollectionName)').val()
            };
            addAntiForgeryToken(data);
            return data;
        }

        function updateMasterCheckbox() {
            var numChkBoxes = $('#collections-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#collections-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
            $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }

    </script>

</form>